package course.view;

import java.awt.Font; 
import java.text.DecimalFormat; 
import java.text.NumberFormat;

import javax.swing.JFrame;
import javax.swing.JPanel; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.labels.StandardPieSectionLabelGenerator; 
import org.jfree.chart.plot.PiePlot; 
import org.jfree.data.general.DefaultPieDataset;

import course.service.impl.CourseServiceImpl; 
public class PieChartFrame extends JFrame{ 
  static ChartPanel panel; 
  public PieChartFrame(){ 
     DefaultPieDataset data = getDataSet(); 
     JFreeChart chart = ChartFactory.createPieChart3D("成绩统计分析",data,true,false,false); 
    //设置百分比 
     PiePlot pieplot = (PiePlot) chart.getPlot(); 
     DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象，主要是设置小数问题 
     NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象 
     StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);//获得StandardPieSectionLabelGenerator对象 
     pieplot.setLabelGenerator(sp1);//设置饼图显示百分比 
   //没有数据的时候显示的内容 
     pieplot.setNoDataMessage("无数据显示"); 
     pieplot.setCircular(false); 
     pieplot.setLabelGap(0.02D); 
     pieplot.setIgnoreNullValues(true);//设置不显示空值 
     pieplot.setIgnoreZeroValues(true);//设置不显示负值 
     panel=new ChartPanel (chart,true); 
     chart.getTitle().setFont(new Font("宋体",Font.BOLD,25));//设置标题字体 
     PiePlot piePlot= (PiePlot) chart.getPlot();//获取图表区域对象 
     piePlot.setLabelFont(new Font("宋体",Font.BOLD,20));//解决乱码 
     chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,15)); 
     this.setSize(1100,800);
     this.setTitle("饼状图");
     this.add(panel);
     this.setVisible(true);
  } 
  private static DefaultPieDataset getDataSet() { 
    DefaultPieDataset dataset = new DefaultPieDataset(); 
    dataset.setValue("0~59",new CourseServiceImpl().getN()); 
    dataset.setValue("60~69",new CourseServiceImpl().getO()); 
    dataset.setValue("70~85",new CourseServiceImpl().getP()); 
    dataset.setValue("86~100",new CourseServiceImpl().getQ()); 
    return dataset; 
} 
  public static ChartPanel getChartPanel(){ 
    return panel; 
  } 
}
